let FriendModel = require('../model').friendModel;
let DaoTool = require('../lib/daoTool');
exports.isFriend = function (conn,params) {
    return new Promise(function (resolve, reject) {
        conn.query("select * from friend where openid=? and FriendOpenId=?", [params.openid,params.FriendOpenId], function (err, result) {
            printLog("=isFriend=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
exports.createFriend = function (conn,params) {
    return new Promise(function (resolve, reject) {
        let sql = "insert into friend set ?";
        conn.query(sql, [DaoTool.generateModelObject(params, FriendModel, 'create')], function (err, result) {
            printLog("=createFriend=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.queryFriend = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query("select * from friend where FriendId=?", [params.FriendId], function (err, result) {
            printLog("=queryFriend=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
exports.queryFriendInfo = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query("select * from friend where openid=? and FriendOpenId=?;", [params.openid,params.TaOpenId], function (err, result) {
            printLog("=queryFriendInfo=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
exports.updateFriendInfo = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query("update friend set ?  where openid=? and FriendOpenId=?;", [DaoTool.generateModelObject(params, FriendModel, 'update'),params.openid,params.TaOpenId], function (err, result) {
            printLog("=queryFriendInfo=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
exports.blackFriend = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query("update friend set IsBlack=? where openid=? and FriendOpenId=?", [params.IsBlack,params.openid,params.FriendOpenId], function (err, result) {
            printLog("=blackFriend=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};
exports.deleteFriend = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query("delete from friend where (openid=? and FriendOpenId=?) or (openid=? and FriendOpenId=?);", [params.openid,params.FriendOpenId,params.FriendOpenId,params.openid], function (err, result) {
            printLog("=deleteFriend=", this.sql);
            if (err) reject(err);
            resolve(result);
        });
    });
};

exports.batchInsertFriend = function (conn,params) {
    return new Promise(function (resolve, reject) {
        let sql = "insert into friend(??) values ?;";
        conn.query(sql, [FriendModel.batchInsertProp,params], function (err, result) {
            printLog("=batchInsertFriend=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
//获取好友列表
exports.selectFriend = function (conn,params,isCount) {
    return new Promise(function (resolve, reject) {
        let sql = " from friend AS frd left join user as u ON u.openid = frd.FriendOpenid LEFT JOIN friend as frd2 on frd2.openid = frd.FriendOpenid and frd2.FriendOpenid = frd.openid left join my_option as myo on myo.openid=frd.FriendOpenid left join ta_option as tao on tao.openid = frd.FriendOpenid where frd.openid= ? AND frd.FriendOpenid NOT in(select BlackOpenId from black_list as bkl where bkl.openid = ?) "+ getSelectUserWhere(params.params);
        let selectSql = "select u.*,frd2.IsBlack,myo.MyField147 as Birthday,myo.MyField148 as Height," + formatMatchingSqlStr(params.selfInfo) + sql + " order by ToMe desc" +DaoTool.getPageParamsSqlStr(params.params)+";";
        let countSql = "select count(1) as num" + sql;
        conn.query(selectSql, [params.params.openid,params.params.openid], function (err, selectResult) {
            printLog("=selectFriend err=",err);
            printLog("=selectFriend=",this.sql);
            if (err) reject(err);
            if (!isCount) resolve(selectResult);
            conn.query(countSql,[params.params.openid,params.params.openid],function (err,countResult) {
                printLog("=countSelectFriend err=",err);
                printLog("=countSelectFriend=",this.sql);
                if (err) reject(err);
                resolve({data:selectResult,count:countResult[0].num});
            });
        });
    });
};
function getSelectUserWhere(params) {
    let sql = "";
    if (params.SelectType) {
        if (params.SelectType == "TheCity") {
            if (params.SelectValue) {
                sql += ' and (u.UserNo = ' + mysqlHelper.escape(params.SelectValue) + ' or u.NickName like ' + mysqlHelper.escape('%' + params.SelectValue + '%') + ') and u.Province=' + params.selfInfo.Province + ' and u.City=' + params.selfInfo.City;
            } else {
                sql += ' and u.Province=' + params.selfInfo.Province + ' and u.City=' + params.selfInfo.City;
            }
        } else if (params.SelectType == "WholeCountry") {
            if (params.SelectValue) {
                sql += ' and (u.UserNo = ' + mysqlHelper.escape(params.SelectValue) + ' or u.NickName like ' + mysqlHelper.escape('%' + params.SelectValue + '%') + ') '
            }
        }
    } else {
        if (params.SelectValue) {
            sql += ' and (u.UserNo = ' + mysqlHelper.escape(params.SelectValue) + ' or u.NickName like ' + mysqlHelper.escape('%' + params.SelectValue + '%') + ') '
        }
        sql += params.UserNo ? " and u.UserNo = " + mysqlHelper.escape(params.UserNo) : "";
        sql += params.Province ? " and u.Province = " + params.Province : "";
        sql += (params.City || params.City===0)? " and u.City = " + params.City : "";
    }
    return sql;
}

function formatMatchingSqlStr(params) {
    try {
        let myo_sql = "";
        let tao_sql = "";
        for (let k = 0; k <= 68; k++) {
            let i = config.questionArr[k];
            if (!params["MyField" + i] || !params["TaField" + i])
                continue;
            if (myo_sql) {
                myo_sql += "+";
                tao_sql += "+";
            }
            myo_sql += '(case when myo.MyField' + i + ' is null then 0 else (matching(myo.MyField' + i + ',"' + params["TaField" + i] + '")) end)';
            tao_sql += '(case when tao.TaField' + i + ' is null then 0 else (matching("' + params["MyField" + i] + '",tao.TaField' + i + ')) end)';
        }
        myo_sql = "((" +(myo_sql?myo_sql:0)  + ")/68) as ToMe";
        tao_sql = "((" + (tao_sql?tao_sql:0) + ")/68) as ToTa ";
        return myo_sql + "," + tao_sql;
    } catch (err) {
        console.log(err);
    }
}